package com.kymatrix.kycarbon.xsmd.system.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.role.InsertRoleReq;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.role.ListQueryRoleResp;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.role.PageQueryRoleReq;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.role.PageQueryRoleResp;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.role.QueryRoleResp;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.role.UpdateRoleReq;
import com.kymatrix.kycarbon.xsmd.system.server.service.IRoleService;
import com.kymatrix.kycarbon.xsmd.common.business.common.PageResp;
import com.kymatrix.kycarbon.xsmd.common.business.common.ResultResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@Api(tags = "角色管理")
public class RoleController {

    @Resource
    IRoleService roleService;

    @ApiOperation("添加角色")
    @PostMapping("/role/add")
    public ResultResp<String> addRole(@RequestBody @Validated InsertRoleReq role) {
        roleService.save(role);
        return ResultResp.ok();
    }

    @ApiOperation("编辑角色")
    @PostMapping("/role/update")
    public ResultResp<String> updateRole(@RequestBody @Validated UpdateRoleReq req) {
        roleService.updateRole(req);
        return ResultResp.ok();
    }

    @ApiOperation("删除角色")
    @GetMapping("/role/delete")
    public ResultResp<String> deleteRole(@RequestParam("id") String id) {
        roleService.deleteRole(id);
        return ResultResp.ok();
    }

    @ApiOperation("分页查询角色列表")
    @PostMapping("/role/pagQuery")
    public ResultResp<PageResp<PageQueryRoleResp>> pagQuery(@RequestBody @Validated PageQueryRoleReq req) {
        Page<PageQueryRoleResp> res = roleService.pageQuery(req);
        return ResultResp.ok(PageResp.build(res));
    }

    @ApiOperation("获取角色列表")
    @GetMapping("/role/list")
    public ResultResp<List<ListQueryRoleResp>> pagQuery(@RequestParam(value = "name", required = false) String name) {
        List<ListQueryRoleResp> res = roleService.list(name);
        return ResultResp.ok(res);
    }

    @ApiOperation("获取角色信息")
    @GetMapping("/role/detail")
    public ResultResp<QueryRoleResp> getRole(@RequestParam("id") String id) {
        QueryRoleResp res = roleService.getRole(id);
        return ResultResp.ok(res);
    }

}
